{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查询知识图谱并返还结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from py2neo import Graph\n",
    "\n",
    "class AnswerSearcher:\n",
    "    def __init__(self):\n",
    "        # self.g = Graph(\"http://localhost:7474\", username=\"neo4j\", password=\"123456\")\n",
    "        self.g = Graph(\"http://localhost:7474\", auth=(\"neo4j\", \"123456\"))\n",
    "        self.num_limit = 20\n",
    "\n",
    "    '''执行cypher查询，并返回相应结果'''\n",
    "    def search_main(self, sqls):\n",
    "        final_answers = []\n",
    "        for sql_ in sqls:\n",
    "            question_type = sql_['question_type']\n",
    "            queries = sql_['sql']\n",
    "            answers = []\n",
    "            for query in queries:\n",
    "                ress = self.g.run(query).data()\n",
    "                answers += ress\n",
    "            final_answer = self.answer_prettify(question_type, answers)\n",
    "            if final_answer:\n",
    "                final_answers.append(final_answer)\n",
    "        return final_answers\n",
    "\n",
    "    '''根据对应的qustion_type，调用相应的回复模板'''\n",
    "    def answer_prettify(self, question_type, answers):\n",
    "        final_answer = []\n",
    "        if not answers:\n",
    "            return ''\n",
    "        if question_type == 'disease_symptom':\n",
    "            desc = [i['n.name'] for i in answers]\n",
    "            subject = answers[0]['m.name']\n",
    "            final_answer = '{0}的症状包括：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))\n",
    "\n",
    "        elif question_type == 'symptom_disease':\n",
    "            desc = [i['m.name'] for i in answers]\n",
    "            subject = answers[0]['n.name']\n",
    "            final_answer = '症状{0}可能染上的疾病有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))\n",
    "\n",
    "        elif question_type == 'disease_cause':\n",
    "            desc = [i['m.cause'] for i in answers]\n",
    "            subject = answers[0]['m.name']\n",
    "            final_answer = '{0}可能的成因有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))\n",
    "\n",
    "        elif question_type == 'disease_acompany':\n",
    "            desc1 = [i['n.name'] for i in answers]\n",
    "            desc2 = [i['m.name'] for i in answers]\n",
    "            subject = answers[0]['m.name']\n",
    "            desc = [i for i in desc1 + desc2 if i != subject]\n",
    "            final_answer = '{0}的症状包括：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))\n",
    "\n",
    "        elif question_type == 'disease_drug':\n",
    "            desc = [i['n.name'] for i in answers]\n",
    "            subject = answers[0]['m.name']\n",
    "            final_answer = '{0}通常的使用的药品包括：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))\n",
    "\n",
    "        elif question_type == 'drug_disease':\n",
    "            desc = [i['m.name'] for i in answers]\n",
    "            subject = answers[0]['n.name']\n",
    "            final_answer = '{0}主治的疾病有{1},可以试试'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))\n",
    "\n",
    "        return final_answer\n",
    "\n",
    "searcher = AnswerSearcher()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 根据问题的类型，进行不同的查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "class QuestionPaser:\n",
    "\n",
    "    '''构建实体节点'''\n",
    "    def build_entitydict(self, args):\n",
    "        entity_dict = {}\n",
    "        for arg, types in args.items():\n",
    "            for type in types:\n",
    "                if type not in entity_dict:\n",
    "                    entity_dict[type] = [arg]\n",
    "                else:\n",
    "                    entity_dict[type].append(arg)\n",
    "\n",
    "        return entity_dict\n",
    "\n",
    "    '''解析主函数'''\n",
    "    def parser_main(self, res_classify):\n",
    "        args = res_classify['args']\n",
    "        entity_dict = self.build_entitydict(args)\n",
    "        question_types = res_classify['question_types']\n",
    "        sqls = []\n",
    "        for question_type in question_types:\n",
    "            sql_ = {}\n",
    "            sql_['question_type'] = question_type\n",
    "            sql = []\n",
    "            if question_type == 'disease_symptom':\n",
    "                sql = self.sql_transfer(question_type, entity_dict.get('disease'))\n",
    "\n",
    "            elif question_type == 'symptom_disease':\n",
    "                sql = self.sql_transfer(question_type, entity_dict.get('symptom'))\n",
    "\n",
    "            elif question_type == 'disease_cause':\n",
    "                sql = self.sql_transfer(question_type, entity_dict.get('disease'))\n",
    "\n",
    "            elif question_type == 'disease_acompany':\n",
    "                sql = self.sql_transfer(question_type, entity_dict.get('disease'))\n",
    "\n",
    "            elif question_type == 'disease_drug':\n",
    "                sql = self.sql_transfer(question_type, entity_dict.get('disease'))\n",
    "\n",
    "            elif question_type == 'drug_disease':\n",
    "                sql = self.sql_transfer(question_type, entity_dict.get('drug'))\n",
    "\n",
    "            if sql:\n",
    "                sql_['sql'] = sql\n",
    "\n",
    "                sqls.append(sql_)\n",
    "\n",
    "        return sqls\n",
    "\n",
    "    '''针对不同的问题，分开进行处理'''\n",
    "    def sql_transfer(self, question_type, entities):\n",
    "        if not entities:\n",
    "            return []\n",
    "\n",
    "        # 查询语句\n",
    "        sql = []\n",
    "        # 查询疾病的原因\n",
    "        if question_type == 'disease_cause':\n",
    "            sql = [\"MATCH (m:Disease) where m.name = '{0}' return m.name, m.cause\".format(i) for i in entities]\n",
    "\n",
    "        # 查询疾病的防御措施\n",
    "        elif question_type == 'disease_prevent':\n",
    "            sql = [\"MATCH (m:Disease) where m.name = '{0}' return m.name, m.prevent\".format(i) for i in entities]\n",
    "\n",
    "        # 查询疾病有哪些症状\n",
    "        elif question_type == 'disease_symptom':\n",
    "            sql = [\"MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where m.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "\n",
    "        # 查询症状可能是由哪些疾病引起的\n",
    "        elif question_type == 'symptom_disease':\n",
    "            sql = [\"MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "\n",
    "        # 查询疾病的并发症\n",
    "        elif question_type == 'disease_acompany':\n",
    "            sql1 = [\"MATCH (m:Disease)-[r:acompany_with]->(n:Disease) where m.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "            sql2 = [\"MATCH (m:Disease)-[r:acompany_with]->(n:Disease) where n.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "            sql = sql1 + sql2\n",
    "\n",
    "        # 查询疾病常用药品－药品别名记得扩充\n",
    "        elif question_type == 'disease_drug':\n",
    "            sql1 = [\"MATCH (m:Disease)-[r:drug]->(n:Drug) where m.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "            sql2 = [\"MATCH (m:Disease)-[r:drug]->(n:Drug) where m.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "            sql = sql1 + sql2\n",
    "\n",
    "        # 已知药品查询能够治疗的疾病\n",
    "        elif question_type == 'drug_disease':\n",
    "            sql1 = [\"MATCH (m:Disease)-[r:drug]->(n:Drug) where n.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "            sql2 = [\"MATCH (m:Disease)-[r:drug]->(n:Drug) where n.name = '{0}' return m.name, r.name, n.name\".format(i) for i in entities]\n",
    "            sql = sql1 + sql2\n",
    "\n",
    "        return sql"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将问题解析分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import ahocorasick\n",
    "\n",
    "class QuestionClassifier:\n",
    "    def __init__(self):\n",
    "        # cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])\n",
    "        cur_dir = os.getcwd()\n",
    "        #　特征词路径\n",
    "        self.disease_path = os.path.join(cur_dir, '../dict/disease.txt')\n",
    "        self.department_path = os.path.join(cur_dir, '../dict/department.txt')\n",
    "        self.check_path = os.path.join(cur_dir, '../dict/check.txt')\n",
    "        self.drug_path = os.path.join(cur_dir, '../dict/drug.txt')\n",
    "        self.food_path = os.path.join(cur_dir, '../dict/food.txt')\n",
    "        self.producer_path = os.path.join(cur_dir, '../dict/producer.txt')\n",
    "        self.symptom_path = os.path.join(cur_dir, '../dict/symptom.txt')\n",
    "        self.deny_path = os.path.join(cur_dir, '../dict/deny.txt')\n",
    "\n",
    "        # 加载特征词\n",
    "        self.disease_wds= [i.strip() for i in open(self.disease_path,encoding=\"utf-8\") if i.strip()]#encoding=\"utf-8\"\n",
    "        self.department_wds= [i.strip() for i in open(self.department_path,encoding=\"utf-8\") if i.strip()]\n",
    "        self.check_wds= [i.strip() for i in open(self.check_path,encoding=\"utf-8\") if i.strip()]\n",
    "        self.drug_wds= [i.strip() for i in open(self.drug_path,encoding=\"utf-8\") if i.strip()]\n",
    "        self.food_wds= [i.strip() for i in open(self.food_path,encoding=\"utf-8\") if i.strip()]\n",
    "        self.producer_wds= [i.strip() for i in open(self.producer_path,encoding=\"utf-8\") if i.strip()]\n",
    "        self.symptom_wds= [i.strip() for i in open(self.symptom_path,encoding=\"utf-8\") if i.strip()]\n",
    "        self.region_words = set(self.department_wds + self.disease_wds + self.check_wds + self.drug_wds + self.food_wds + self.producer_wds + self.symptom_wds)\n",
    "        self.deny_words = [i.strip() for i in open(self.deny_path,encoding=\"utf-8\") if i.strip()]\n",
    "        # 构造领域actree\n",
    "        self.region_tree = self.build_actree(list(self.region_words))\n",
    "        # 构建词典\n",
    "        self.wdtype_dict = self.build_wdtype_dict()\n",
    "        # 问句疑问词\n",
    "        self.symptom_qwds = ['症状', '表征', '现象', '症候', '表现']\n",
    "        self.cause_qwds = ['原因','成因', '为什么', '怎么会', '怎样才', '咋样才', '怎样会', '如何会', '为啥', '为何', '如何才会', '怎么才会', '会导致', '会造成']\n",
    "        self.acompany_qwds = ['并发症', '并发', '一起发生', '一并发生', '一起出现', '一并出现', '一同发生', '一同出现', '伴随发生', '伴随', '共现']\n",
    "        self.food_qwds = ['饮食', '饮用', '吃', '食', '伙食', '膳食', '喝', '菜' ,'忌口', '补品', '保健品', '食谱', '菜谱', '食用', '食物','补品']\n",
    "        self.drug_qwds = ['药', '药品', '用药', '胶囊', '口服液', '炎片']\n",
    "        self.prevent_qwds = ['预防', '防范', '抵制', '抵御', '防止','躲避','逃避','避开','免得','逃开','避开','避掉','躲开','躲掉','绕开',\n",
    "                             '怎样才能不', '怎么才能不', '咋样才能不','咋才能不', '如何才能不',\n",
    "                             '怎样才不', '怎么才不', '咋样才不','咋才不', '如何才不',\n",
    "                             '怎样才可以不', '怎么才可以不', '咋样才可以不', '咋才可以不', '如何可以不',\n",
    "                             '怎样才可不', '怎么才可不', '咋样才可不', '咋才可不', '如何可不']\n",
    "        self.lasttime_qwds = ['周期', '多久', '多长时间', '多少时间', '几天', '几年', '多少天', '多少小时', '几个小时', '多少年']\n",
    "        self.cureway_qwds = ['怎么治疗', '如何医治', '怎么医治', '怎么治', '怎么医', '如何治', '医治方式', '疗法', '咋治', '怎么办', '咋办', '咋治']\n",
    "        self.cureprob_qwds = ['多大概率能治好', '多大几率能治好', '治好希望大么', '几率', '几成', '比例', '可能性', '能治', '可治', '可以治', '可以医']\n",
    "        self.easyget_qwds = ['易感人群', '容易感染', '易发人群', '什么人', '哪些人', '感染', '染上', '得上']\n",
    "        self.check_qwds = ['检查', '检查项目', '查出', '检查', '测出', '试出']\n",
    "        self.belong_qwds = ['属于什么科', '属于', '什么科', '科室']\n",
    "        self.cure_qwds = ['治疗什么', '治啥', '治疗啥', '医治啥', '治愈啥', '主治啥', '主治什么', '有什么用', '有何用', '用处', '用途',\n",
    "                          '有什么好处', '有什么益处', '有何益处', '用来', '用来做啥', '用来作甚', '需要', '要']\n",
    "\n",
    "        print('model init finished ......')\n",
    "\n",
    "        return\n",
    "\n",
    "    '''分类主函数'''\n",
    "    def classify(self, question):\n",
    "        data = {}\n",
    "        medical_dict = self.check_medical(question)\n",
    "        if not medical_dict:\n",
    "            return {}\n",
    "        data['args'] = medical_dict\n",
    "        # 收集问句当中所涉及到的实体类型\n",
    "        types = []\n",
    "        for type_ in medical_dict.values():\n",
    "            types += type_\n",
    "        question_type = 'others'\n",
    "\n",
    "        question_types = []\n",
    "\n",
    "        # 症状\n",
    "        if self.check_words(self.symptom_qwds, question) and ('disease' in types):\n",
    "            question_type = 'disease_symptom'\n",
    "            question_types.append(question_type)\n",
    "\n",
    "        if self.check_words(self.symptom_qwds, question) and ('symptom' in types):\n",
    "            question_type = 'symptom_disease'\n",
    "            question_types.append(question_type)\n",
    "\n",
    "        # 原因\n",
    "        if self.check_words(self.cause_qwds, question) and ('disease' in types):\n",
    "            question_type = 'disease_cause'\n",
    "            question_types.append(question_type)\n",
    "        # 并发症\n",
    "        if self.check_words(self.acompany_qwds, question) and ('disease' in types):\n",
    "            question_type = 'disease_acompany'\n",
    "            question_types.append(question_type)\n",
    "\n",
    "        # 推荐药品\n",
    "        if self.check_words(self.drug_qwds, question) and 'disease' in types:\n",
    "            question_type = 'disease_drug'\n",
    "            question_types.append(question_type)\n",
    "\n",
    "        # 药品治啥病\n",
    "        if self.check_words(self.cure_qwds, question) and 'drug' in types:\n",
    "            question_type = 'drug_disease'\n",
    "            question_types.append(question_type)\n",
    "\n",
    "        # 将多个分类结果进行合并处理，组装成一个字典\n",
    "        data['question_types'] = question_types\n",
    "\n",
    "        return data\n",
    "\n",
    "    '''构造词对应的类型'''\n",
    "    def build_wdtype_dict(self):\n",
    "        wd_dict = dict()\n",
    "        for wd in self.region_words:\n",
    "            wd_dict[wd] = []\n",
    "            if wd in self.disease_wds:\n",
    "                wd_dict[wd].append('disease')\n",
    "            if wd in self.department_wds:\n",
    "                wd_dict[wd].append('department')\n",
    "            if wd in self.drug_wds:\n",
    "                wd_dict[wd].append('drug')\n",
    "            if wd in self.symptom_wds:\n",
    "                wd_dict[wd].append('symptom')\n",
    "            if wd in self.producer_wds:\n",
    "                wd_dict[wd].append('producer')\n",
    "        return wd_dict\n",
    "\n",
    "    '''构造actree，加速过滤'''\n",
    "    def build_actree(self, wordlist):\n",
    "        actree = ahocorasick.Automaton()\n",
    "        for index, word in enumerate(wordlist):\n",
    "            actree.add_word(word, (index, word))\n",
    "        actree.make_automaton()\n",
    "        return actree\n",
    "\n",
    "    '''问句过滤'''\n",
    "    def check_medical(self, question):\n",
    "        region_wds = []\n",
    "        for i in self.region_tree.iter(question):\n",
    "            wd = i[1][1]\n",
    "            region_wds.append(wd)\n",
    "        stop_wds = []\n",
    "        for wd1 in region_wds:\n",
    "            for wd2 in region_wds:\n",
    "                if wd1 in wd2 and wd1 != wd2:\n",
    "                    stop_wds.append(wd1)\n",
    "        final_wds = [i for i in region_wds if i not in stop_wds]\n",
    "        final_dict = {i:self.wdtype_dict.get(i) for i in final_wds}\n",
    "\n",
    "        return final_dict\n",
    "\n",
    "    '''基于特征词进行分类'''\n",
    "    def check_words(self, wds, sent):\n",
    "        for wd in wds:\n",
    "            if wd in sent:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实例化辅助函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model init finished ......\n"
     ]
    }
   ],
   "source": [
    "classifier = QuestionClassifier()\n",
    "\n",
    "parser = QuestionPaser()\n",
    "\n",
    "searcher = AnswerSearcher()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实验demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "sequence item 0: expected str instance, NoneType found",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mC:\\AppData\\Local\\Temp/ipykernel_29720/4253638810.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mres_classify\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mclassifier\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclassify\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mquestion\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 从问题中提取实体和关系\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mres_cypher\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mparser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparser_main\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mres_classify\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 问题解析\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mfinal_answers\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msearcher\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch_main\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mres_cypher\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 生成回答\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[0mfinal_answers\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\AppData\\Local\\Temp/ipykernel_29720/24878543.py\u001b[0m in \u001b[0;36msearch_main\u001b[1;34m(self, sqls)\u001b[0m\n\u001b[0;32m     17\u001b[0m                 \u001b[0mress\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mquery\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m                 \u001b[0manswers\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mress\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m             \u001b[0mfinal_answer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0manswer_prettify\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mquestion_type\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0manswers\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     20\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mfinal_answer\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m                 \u001b[0mfinal_answers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfinal_answer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\AppData\\Local\\Temp/ipykernel_29720/24878543.py\u001b[0m in \u001b[0;36manswer_prettify\u001b[1;34m(self, question_type, answers)\u001b[0m\n\u001b[0;32m     71\u001b[0m             \u001b[0mdesc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'm.desc'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0manswers\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     72\u001b[0m             \u001b[0msubject\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0manswers\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'm.name'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 73\u001b[1;33m             \u001b[0mfinal_answer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'{0},熟悉一下：{1}'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msubject\u001b[0m\u001b[1;33m,\u001b[0m  \u001b[1;34m'；'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdesc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnum_limit\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     74\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     75\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0mquestion_type\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'disease_acompany'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: sequence item 0: expected str instance, NoneType found"
     ]
    }
   ],
   "source": [
    "question = '肺气肿的病因' # 输入问题\n",
    "res_classify = classifier.classify(question) # 从问题中提取实体和关系\n",
    "res_cypher = parser.parser_main(res_classify) # 问题解析\n",
    "final_answers = searcher.search_main(res_cypher) # 生成回答\n",
    "final_answers"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
